﻿using System;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.SessionState;
using CSS.Action.Core;

namespace CSS.Action.Actions
{
    public class FileManagerAction : ActionBase
    {
        public String RootPath = WebConfigurationManager.AppSettings["FileServer.Upload"];
        public String FolderItem = String.Empty;

        protected override string ResponseContentType
        {
            get { return "text/plain; charset=utf-8"; }
        }

        protected override bool IsResponseDataDirectly
        {
            get { return false; }
        }

        protected override object ProcessAction(string functionName, HttpContext context)
        {
            var requestItem = !String.IsNullOrEmpty(HttpContext.Current.Request["folder"])
                                  ? Int32.Parse(HttpContext.Current.Request["folder"].Trim())
                                  : 1;
            switch (requestItem)
            {
                case 1:
                    FolderItem = "photo";
                    break;
                case 2:
                    FolderItem = "app";
                    break;
                case 3:
                    FolderItem = "video";
                    break;
                case 4:
                    FolderItem = "user";
                    break;
                default:
                    FolderItem = "file";
                    break;
            }
            var responseData = new ResponseData();
            switch (functionName)
            {
                case "upload":
                    responseData = UploadFile();
                    break;
            }
            return responseData;
        }

        private ResponseData UploadFile()
        {
            var responseData = new ResponseData();

            var folderItemId = !String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["f"])
                                   ? HttpContext.Current.Request.QueryString["f"]
                                   : "";
            var cacheName = "DirectoriesAndFilesList_" + folderItemId.Replace("/", "_");

            #region Process

            var uploadService = new UploadService();
            if (HttpContext.Current.Request.Files["Filedata"] != null)
            {
                var httpPostedFile = HttpContext.Current.Request.Files["Filedata"];
                try
                {
                    var buffer = new Byte[httpPostedFile.ContentLength];
                    httpPostedFile.InputStream.Read(buffer, 0, httpPostedFile.ContentLength);
                    var filePath = uploadService.Upload(buffer, FolderItem,
                                                        Path.GetExtension(httpPostedFile.FileName), folderItemId);
                    responseData.Success = true;
                    responseData.Data = "/" + filePath;
                    HttpContext.Current.Cache.Remove(cacheName);
                }
                catch (Exception ex)
                {
                    responseData.Success = false;
                    responseData.Message = ex.ToString();
                }
            }
            #endregion

            return responseData;
        }
    }
}